package dd.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import xml.Message;

import dd.model.Event;
import dd.model.Model;
import dd.view.ModeratorAddChoices;


public class closeChoiceConfirmButtonController implements ActionListener{
	/**
	 * after the moderator set all the choices for a close event
	 * call this controller when he press the confirm button
	 * send all the choices to the server with all the information one event has
	 */
	
	ModeratorAddChoices mac;
	Event event;
	
	public closeChoiceConfirmButtonController (ModeratorAddChoices mac, Event event) {
		this.mac = mac;
		this.event = event;
	}
	
	public void actionPerformed(ActionEvent arg0) {
		Model model = Model.getInstance();
		event = model.getEvent();
		String msgchoice =  "<choice value='"+mac.getTextField(0).getText()+"' index='0'/>";
		this.event.setChoice(0, mac.getTextField(0).getText());
		for(int i = 1; i < this.event.getNumberLines(); i++) {
			msgchoice =msgchoice+ "<choice value='"+mac.getTextField(i).getText()+"' index='"+String.valueOf(i)+"'/>";
			this.event.setChoice(i, mac.getTextField(i).getText());
		}
		String xmlString = Message.requestHeader() + "<createRequest behavior='"+event.getType()+"' type='"+event.getMode()+"' question='"+event.getEventName()+
				"' numChoices='"+event.getNumberLines()+"' numRounds='"+event.getNumberEdges()+"'>"+
			//	"<choice value='efaefa' index='0'/><choice value='222222' index='1'/>"+
				msgchoice+
				"<user name='"+event.getUserName() + "' password='" + event.getPassword() + "'/>" +
				"</createRequest>"+"</request>";
		Message m = new Message (xmlString);
		model.getServerAccess().sendRequest(m);
		model.setmac(mac);
	//	addEdgesScreen aes = new addEdgesScreen(event);
	//	aes.setVisible(true);
	//	mac.dispose();
	}

}
